package com.szgr.baoyin.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.SystemBarTintManager;
import com.umeng.analytics.MobclickAgent;

public abstract class BaseActivity extends FragmentActivity implements View.OnClickListener {
    public ActionBar actionBar;
    public View actionBarLeft;
    public ImageView actionBarRight;
    public TextView actionBarTitle;
    public TextView actionBarCount;
    public TextView actionBarLocation;
    public TextView actionBarConfirm;
    public View view;
    private SystemBarTintManager mTintManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar = getActionBar();
        view = getWindow().getDecorView();
//        if (actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(false);
//            actionBar.setDisplayShowHomeEnabled(false);
//            actionBar.setDisplayShowTitleEnabled(false);
//            actionBar.setDisplayUseLogoEnabled(false);
//
//            actionBar.setDisplayShowCustomEnabled(true);
//            LayoutInflater inflator = (LayoutInflater) this
//                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//            View v = inflator.inflate(R.layout.custom_actionbar_view, null);
//            actionBarLeft = v.findViewById(R.id.action_bar_left);
//            actionBarLeft.setOnClickListener(this);
//            actionBarTitle = (TextView) v.findViewById(R.id.action_bar_title);
//            actionBarRight = v.findViewById(R.id.action_bar_right);
//            actionBarRight.setOnClickListener(this);
//            ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
//                    ActionBar.LayoutParams.MATCH_PARENT,
//                    ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
//            actionBar.setCustomView(v, lp);
//        }
        actionBarLeft = findViewById(R.id.action_bar_left);
        actionBarLeft.setOnClickListener(this);
        actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarRight = (ImageView) findViewById(R.id.action_bar_right);
        actionBarRight.setOnClickListener(this);
        actionBarCount = (TextView) findViewById(R.id.action_bar_count);
        actionBarLocation = (TextView) findViewById(R.id.action_bar_location);
        actionBarConfirm = (TextView) findViewById(R.id.action_bar_confirm);
        actionBarConfirm.setOnClickListener(this);

        initActionBar();
        initView();

        //判断操作系统的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 状态栏透明 需要在创建SystemBarTintManager之前调用。
            setTranslucentStatus(true);//方法如下
        }
        mTintManager = new SystemBarTintManager(this);
        // 设置状态栏背景
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setStatusBarTintResource(R.color.main_blue);//颜色可以随便修改
    }

    public abstract void initActionBar();

    public abstract void initView();

    protected void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        MyApplication.getmInstance().cancelPendingRequests(MyApplication.TAG);
        super.onDestroy();
    }
}
